# encoding=utf8
import sys
import math
reload(sys)
sys.setdefaultencoding('utf8')
from ..obdMessageAnalysis import*

class OBD_02:
    result = []
    analysisDescription = ["数据采集时间:", "信息类型标志:","信息流水号"]
    analysis02Length = [6,1,2]
    CollectTime = ""
    InfFlag = ""
    Login_num = ""
    s=""
    obdMesgstrinfo = ""
    dataFlowinfo = ""
    OBDmesg=[["OBD诊断协议:","2"],["MIL状态:","2"],["诊断支持状态:","4"],["诊断就绪状态:","4"],["车辆识别码:","34"],["软件标定识别号:","36"],["标定验证码（CVN）:","36"],["IUPR值:","72"],["故障码总数:","2"]]
    DataFlow=[["车速:","4"],["大气压力（直接测量或估计值）:","2"],["发动机净输出扭矩:","2"],["摩擦扭矩:","2"],["发动机转速:","4"],["发动机燃料流量:","4"],["SCR上游NOx传感器输出值:","4"],["SCR下游NOx传感器输出值:","4"],["反应剂余量:","2"],["进气量:","4"],["SCR入口温度","4"],["SCR出口温度","4"],["DPF压差","4"],["发动机冷却液温度:","2"],["油箱液位:","2"],["定位状态:","2"],["经度:","8"],["纬度:","8"],["累计里程:","8"]]
    def LogicAisle(self, protocolStr, header):
        print "header02=%s"%header[1].split(':')[1]
        print "protocolStr02=%s"%protocolStr
        print "header02=%s"%header
        count = 0
        for i in range(len(self.analysisDescription)):
            if i==0:
                Str=protocolStr[count:count + (self.analysis02Length[i] * 2)]
                print "Str=%s"%Str
                self.proCollectTime(Str)
                header.append(self.analysisDescription[i] + self.CollectTime + "\n")
                header.append("\n"+"======第1包数据======" + "\n")
            else:
                header.append(self.analysisDescription[i] + protocolStr[count:count + (self.analysis02Length[i] * 2)] + "\n");
            count = count + self.analysis02Length[i] * 2
        #obdmesgstr=protocolStr[(6+1+2)*2:192+int(protocolStr[190:192],16)*4*2]
        self.result = header
        obdmesgstr=protocolStr[(6+1+2)*2:]
        print "obdmesgstr1=%s"%obdmesgstr
        print "obdmesgstrlen1=%d"%len(obdmesgstr)
        strindex1=self.proObdMesg(obdmesgstr)
        dataflowstr=protocolStr[strindex1:]
        strindex2=self.prodataFlow(dataflowstr,strindex1)
        print "strindex1=%d"%strindex1
        print "strindex2=%d"%strindex2
        #strindex3=strindex1+strindex2
        strindexfaultcode=strindex2
        for i in range(9):
            ff=i+2
            self.result.append("\n"+"======第"+str(ff)+"包数据======" + "\n")
            obdmesgstr=protocolStr[strindex2:]
            print "第%s包"%str(ff)
            print "obdmesgstr=%s"%obdmesgstr
            print "obdmesgstrlen=%d"%len(obdmesgstr)
            print "hhhhhhhhhhhhhhhstrindex2=%d"%strindex2
            strindex3=self.proObdMesg2(obdmesgstr,strindex2)
            #dataflowstr=protocolStr[strindex2:]
            print strindex3
            dataflowstr=obdmesgstr[strindex3[0]:]
            print "dataflowstr=%s"%dataflowstr
            strindex4=self.prodataFlow2(dataflowstr,strindex3[0])
            strindex2=strindexfaultcode+strindex4*(i+1)
            print "555555555555555555555strindex2=%d"%strindex2
        self.result = header
    def proObdMesg(self,obdmesgstr):
        tmp =[]
        tmp=self.OBDmesg
        datalen=0
        startindex=0
        for i in range(len(tmp)):
            print "proObdMesg.i=%d"%i
            datalen +=int(tmp[i][1])
            print "proObdMesg.datalen=%d"%datalen
            self.obdMesgstrinfo=obdmesgstr[startindex:datalen]
            print "proObdMesg.self.obdMesgstrinfo=%s"%self.obdMesgstrinfo
            startindex +=int(tmp[i][1])
            print "proObdMesg.startindex=%d"%startindex
            self.result.append(tmp[i][0] + self.obdMesgstrinfo + "\n");
            strindex=192+18
            try:
                if i==8 and int(self.obdMesgstrinfo,16)!=0 and int(self.obdMesgstrinfo,16)!=254:
                    self.result.append("故障码信息列表:" + obdmesgstr[datalen:datalen+int(self.obdMesgstrinfo,16)*8] + "\n");
                    strindex=datalen+int(self.obdMesgstrinfo,16)*8+18
            except Exception as e:
                print(e)
        print "proObdMesg.strindex=%d"%strindex
        return strindex
    def prodataFlow(self,dataflowstr,strindex):
        self.result.append(self.analysisDescription[1] + dataflowstr[0:2] + "\n");
        print "prodataFlow.dataflowstr=%s"%dataflowstr
        dataflowstr=dataflowstr[2:]
        tmp =[]
        tmp=self.DataFlow
        datalen=0
        startindex=0
        for i in range(len(tmp)):
            print "prodataFlow.i=%d"%i
            datalen +=int(tmp[i][1])
            print "prodataFlow.datalen=%d"%datalen
            self.dataFlowinfo=dataflowstr[startindex:datalen]
            print "prodataFlow.self.dataFlowinfo=%s"%self.dataFlowinfo
            startindex +=int(tmp[i][1])
            print "prodataFlow.startindex=%d"%startindex
            self.result.append(tmp[i][0] + self.dataFlowinfo + "\n");
        strindex=strindex+datalen+2#2是信息类型标志的长度
        print "prodataFlow.strindex=%d"%strindex
        return strindex
    def proObdMesg2(self,obdmesgstr,strindex):
        self.result.append(self.analysisDescription[1] + obdmesgstr[0:2] + "\n");
        print "proObdMesg2.obdmesgstr=%s"%obdmesgstr
        obdmesgstr=obdmesgstr[2:]
        tmp =[]
        tmp=self.OBDmesg
        datalen=0
        startindex=0
        for i in range(len(tmp)):
            print "proObdMesg.i=%d"%i
            datalen +=int(tmp[i][1])
            print "proObdMesg.datalen=%d"%datalen
            self.obdMesgstrinfo=obdmesgstr[startindex:datalen]
            print "proObdMesg.self.obdMesgstrinfo=%s"%self.obdMesgstrinfo
            startindex +=int(tmp[i][1])
            print "proObdMesg.startindex=%d"%startindex
            self.result.append(tmp[i][0] + self.obdMesgstrinfo + "\n");
            #strindex=strindex+startindex
            faultcodelen=0
            try:
                if i==8 and int(self.obdMesgstrinfo,16)!=0 and int(self.obdMesgstrinfo,16)!=254:
                    self.result.append("故障码信息列表:" + obdmesgstr[datalen:datalen+int(self.obdMesgstrinfo,16)*8] + "\n");
                    startindex=datalen+int(self.obdMesgstrinfo,16)*8
                    faultcodelen=len(obdmesgstr[datalen:datalen+int(self.obdMesgstrinfo,16)*8])
            except Exception as e:
                print(e)
        print "proObdMesg2.startindex=%d"%startindex
        return startindex+2,faultcodelen#2是信息类型标志的长度
    def prodataFlow2(self,dataflowstr,strindex):
        self.result.append(self.analysisDescription[1] + dataflowstr[0:2] + "\n");
        dataflowstr=dataflowstr[2:]
        tmp =[]
        tmp=self.DataFlow
        datalen=0
        startindex=0
        for i in range(len(tmp)):
            print "prodataFlow.i=%d"%i
            datalen +=int(tmp[i][1])
            print "prodataFlow.datalen=%d"%datalen
            self.dataFlowinfo=dataflowstr[startindex:datalen]
            print "prodataFlow.self.dataFlowinfo=%s"%self.dataFlowinfo
            startindex +=int(tmp[i][1])
            print "prodataFlow.startindex=%d"%startindex
            self.result.append(tmp[i][0] + self.dataFlowinfo + "\n");
        strindex=strindex+datalen+2#2是信息类型标志的长度
        print "prodataFlow2.strindex=%d"%strindex
        return strindex
    def proCollectTime(self,timestr):
        i=0
        timelist =[]
        while i<len(timestr):
            print "proCollectTime=%d"%i
            print "proCollectTimelen=%d"%len(timestr)
            timestrnew=timestr[i:i+2]
            i=i+2
            timelist.append(str(int(timestrnew,16)))
            print "timelist=%s"%timelist
        timelist.insert(0, "20")
        timelist.insert(2, "年")
        timelist.insert(4, "月")
        timelist.insert(6, "日")
        timelist.insert(8, "时")
        timelist.insert(10, "分")
        timelist.insert(12, "秒")
        self.CollectTime = "".join(timelist)
